﻿using System;
using System.Collections;
using System.Collections.Generic;

using IBatisNet.DataAccess;

using AUV5.Model;
using AUV5.DAL;
using AUV5.DAL.Interface;
using AUV5.DAL.Dao;

namespace AUV5.BLL
{
    /// <summary>
    /// 功能：     ServiceCategory业务逻辑实现
    /// 作者：     lwl
    /// 日期：     2011/10/14
    /// 修改日期： 
    /// 修改人：
    /// 修改内容：
    /// </summary>
    public class ServiceCategoryBLL
    {
        private static ServiceCategoryBLL instance = null;
        private static object syncRoot = new object();
        private IDaoManager daoManager = ServiceConfig.Instance.DaoManager;
        private IServiceCategoryDao dao = null;

        private ServiceCategoryBLL()
        {
            dao = ServiceCategorySqlMapDao.Instance;
        }

        public static ServiceCategoryBLL Instance
        {
            get
            {
                if (instance == null)
                {
                    lock (syncRoot)
                    {
                        if (instance == null)
                        {
                            instance = new ServiceCategoryBLL();
                        }
                    }
                }
                return instance;
            }
        }

        /// <summary>
        /// 得到最大行数
        /// </summary>
        /// <returns></returns>
        public int GetMaxCounts()
        {
            try
            {
                //daoManager.BeginTransaction();

                int count = dao.GetMaxCounts();

                //daoManager.CommitTransaction();

                return count;
            }
            catch
            {
                //daoManager.RollBackTransaction();
                throw;
            }
        }

        /// <summary>
        /// 得到列表
        /// </summary>
        /// <returns></returns>
        public System.Collections.Generic.IList<ServiceCategory> GetList()
        {
            try
            {
                //daoManager.BeginTransaction();

                System.Collections.Generic.IList<ServiceCategory> list = dao.GetList();

                //daoManager.CommitTransaction();

                return list;
            }
            catch
            {
                //daoManager.RollBackTransaction();
                throw;
            }
        }

        /// <summary>
        /// 通过不同参数得到列表
        /// </summary>
        /// <param name="TopCounts"></param>
        /// <returns></returns>
        private System.Collections.Generic.IList<ServiceCategory> GetListByParameters(object TopCounts, ServiceCategory servicecategory)
        {
            try
            {
                //daoManager.BeginTransaction();

                Hashtable ht = new Hashtable();
                ht.Add("TopCounts", TopCounts);
                System.Collections.Generic.IList<ServiceCategory> list = dao.GetListByParameters(ht);

                //daoManager.CommitTransaction();

                return list;
            }
            catch
            {
                //daoManager.RollBackTransaction();
                throw;
            }
        }


        /// <summary>
        /// 得到明细
        /// </summary>
        /// <param name="CategoryID"></param>
        /// <returns></returns>
        public ServiceCategory GetDetail(System.String categoryid)
        {
            try
            {
                ///daoManager.BeginTransaction();
                ServiceCategory category = new ServiceCategory();
                category.CategoryID = categoryid;
                return dao.GetDetail(category);

                ///daoManager.CommitTransaction();
            }
            catch
            {
                ///daoManager.RollBackTransaction();
                throw;
            }
        }

        public ServiceCategory GetCategoryByIDOrCode(string category)
        {
            try
            {
                ServiceCategory scbyid = new ServiceCategory();
                scbyid.CategoryID = category;
                ServiceCategory result = dao.GetDetail(scbyid);
                if (result == null)
                {
                    //根据Code(CategoryName)查找
                    ServiceCategory scbycode = new ServiceCategory();
                    scbycode.CategoryName = category;
                    result = dao.GetDetail(scbycode);
                }
                return result;
            }
            catch
            {
                throw;
            }
        }

        /// <summary>
        /// 新建服务类别，类别名称不可重复
        /// </summary>
        /// <param name="servicecategory"></param>
        /// <returns></returns>
        public object Add(ServiceCategory category)
        {
            try
            {
                ///daoManager.BeginTransaction();
                ServiceCategory ret = dao.GetDetail(category);
                if (ret != null)
                {
                    return ret.CategoryID;
                }
                else
                {
                    category.CategoryID = Guid.NewGuid().ToString();
                    category.IsDel = (int)Common.Enum.auvDel.NotDel;
                    category.State = (int)Common.Enum.auvState.Enabled;
                    category.CreateDate = DateTime.Now;
                    category.UpdateDate = DateTime.Now;
                    category.BeginDate = DateTime.Now;
                    category.EndDate = DateTime.Now;
                    dao.Add(category);
                    return category.CategoryID;
                }

                ///daoManager.CommitTransaction();
            }
            catch
            {
                ///daoManager.RollBackTransaction();
                throw;
            }
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="servicecategory"></param>
        /// <returns></returns>
        public int Update(ServiceCategory servicecategory)
        {
            try
            {
                ///daoManager.BeginTransaction();

                return dao.Update(servicecategory);

                ///daoManager.CommitTransaction();
            }
            catch
            {
                ///daoManager.RollBackTransaction();
                throw;
            }
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="CategoryID"></param>
        /// <returns></returns>
        public int Delete(System.String categoryid)
        {
            try
            {
                ///daoManager.BeginTransaction();

                return dao.Delete(categoryid);

                ///daoManager.CommitTransaction();
            }
            catch
            {
                ///daoManager.RollBackTransaction();
                throw;
            }
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="servicecategory"></param>
        /// <returns></returns>
        public int Delete(ServiceCategory servicecategory)
        {
            try
            {
                ///daoManager.BeginTransaction();

                return dao.Delete(servicecategory);

                ///daoManager.CommitTransaction();
            }
            catch
            {
                ///daoManager.RollBackTransaction();
                throw;
            }
        }


        #region 获取服务类别的JSon数据    add by Vinlon
        /// <summary>
        /// 获取服务类别
        /// </summary>
        /// <returns>JSon字符串</returns>
        public string GetJsonList()
        {
            return dao.GetJsonList();
        }
        #endregion


        //获取未禁用的服务类别列表
        public IList<ServiceCategory> GetActiveList()
        {
            try
            {
                Hashtable ht = new Hashtable();
                ht.Add("state", (int)Common.Enum.auvState.Enabled);
                ht.Add("isDel", (int)Common.Enum.auvDel.NotDel);
                return dao.GetListByParameters(ht);
            }
            catch (Exception)
            {

                throw;
            }
        }
    }
}
